<template>
    <div class="manage-task">
        <div style="height: 35px">
            <el-form :model="searchForm" :inline="true" size="mini">
                <el-form-item label="name">
                    <el-input v-model="searchForm.name"></el-input>
                </el-form-item>
                <el-form-item label="code">
                    <el-input v-model="searchForm.code"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="listTask">Search</el-button>
                    <el-button type="primary" @click="listTask">Refresh</el-button>
                    <el-button type="primary" @click="handleCreate">New</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table
                v-infiniteScroll="infiniteScroll"
                border
                default-expand-all
                highlight-current-row
                :data="taskTable"
                row-key="ukId"
                height="500"
                size="mini"
                style="width: 100%"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">

            <el-table-column
                    prop="name"
                    label="名称"
                    width="*"
                    min-width="150">
            </el-table-column>
            <el-table-column
                    prop="type"
                    label="类型"
                    width="70">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.type == '1'" type="success" size="mini">自定义</el-tag>
                    <el-tag v-if="scope.row.type == '0'" type="danger" size="mini">系统内置</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    prop="cron"
                    label="cron表达式"
                    width="160">
            </el-table-column>
            <el-table-column
                    prop="total"
                    label="总执行次数"
                    width="90">
            </el-table-column>
            <el-table-column
                    prop="count"
                    label="已执行次数"
                    width="90">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="content"
                    label="执行内容"
                    width="120">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="summary"
                    label="简介"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="status"
                    width="70">
                <template slot-scope="scope">
                    <el-switch
                            @change="statusChange($event, scope.row.ukId)"
                            :value="scope.row.status != '1'"
                            active-color="#909399"
                            inactive-color="#67c23a">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                    header-align="left"
                    align="center"
                    prop="sort"
                    label="排序"
                    width="60">
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="220">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.status == '1'"
                               size="mini"
                               type="primary"
                               plain
                               @click="handleStop(scope.$index, scope.row)">停止
                    </el-button>
                    <el-button v-if="scope.row.status == '0'"
                               size="mini"
                               type="primary"
                               plain
                               @click="handleStart(scope.$index, scope.row)">启动
                    </el-button>
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row.ukId)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog title="任务管理" :visible.sync="showTaskForm" :close-on-click-modal="false" label-width="120px">
            <el-form ref="form" :model="taskForm" label-width="120px">
                <el-form-item label="任务名称">
                    <el-input v-model="taskForm.name"></el-input>
                </el-form-item>
                <el-form-item label="任务类型">
                    <el-input v-model="taskForm.type"></el-input>
                </el-form-item>
                <el-form-item label="任务执行时间">
                    <el-input v-model="taskForm.cron"></el-input>
                </el-form-item>
                <el-form-item label="总执行次数">
                    <el-input type="number" v-model="taskForm.total"></el-input>
                </el-form-item>
                <el-form-item label="执行内容">
                    <el-input v-model="taskForm.content"></el-input>
                </el-form-item>
                <el-form-item label="任务简介">
                    <el-input v-model="taskForm.summary"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="taskForm.status" placeholder="请选择状态">
                        <el-option label="启用" :value="1"></el-option>
                        <el-option label="停用" :value="0"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="排序">
                    <el-input v-model="taskForm.sort" type="number" min="0" max="90000"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleSave">保存</el-button>
                    <el-button @click="showTaskForm = false">取 消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
    import {listTask, saveTask, deleteTask, selectTask, startTask, stopTask} from "../../../api/manage/task";
    import PageUtils from "../../../utils/PageUtils";

    export default {
        name: "Task",
        data() {
            return {
                showTaskForm: false,
                taskTable: [],
                searchForm: {},
                taskForm: {},
                pagination: {
                    total: 0,
                    size: 30,
                    current: 1,
                }
            }
        },
        methods: {
            handleCreate() {
                this.taskForm = {
                    status: 1
                }
                this.showTaskForm = true
            },
            handleEdit(row) {
                this.taskForm = row
                this.showTaskForm = true
            },
            handleSave() {
                saveTask(this.taskForm).then(result => {
                    this.listTask()
                    this.showTaskForm = false
                }).catch(error => {
                    console.log(error)
                })
            },
            handleDelete(ukId) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteTask(ukId).then(result => {
                        this.listTask()
                        this.showTaskForm = false
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }).catch(error => {
                        console.log(error)
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            statusChange(value, paginationId) {
                let status = value ? 0 : 1
                updateTaskStatus(paginationId, status).then(result => {
                    this.listTask()
                }).catch(error => {
                    console.log(error)
                })
            },
            handleStart(index, row) {
                startTask(row.ukId).then(response => {
                    this.listTask();
                }).catch(error => {
                    console.log(error)
                })
            },
            handleStop(index, row) {
                stopTask(row.ukId).then(response => {
                    this.listTask();
                }).catch(error => {
                    console.log(error)
                })
            },
            infiniteScroll() {
                let current = this.$store.state.Pabu.pagination.task.current + 1
                let size = this.$store.state.Pabu.pagination.task.size
                listTask(this.searchForm, current, size).then(result => {
                    if (result.data.tasks.length !== 0) {
                        for (let task of result.data.tasks) {
                            this.taskTable.push(task)
                        }
                        PageUtils.setPagination(this, "task", result.data.tasks)
                    }
                }).catch(error => {
                    console.log(error)
                })
            },
            listTask() {
                listTask(this.searchForm, 1, 30).then(result => {
                    this.taskTable = result.data.tasks
                    PageUtils.setPagination(this, "task", result.data.tasks)
                }).catch(error => {
                    console.log(error)
                })
            }
        },
        created() {
            this.listTask()
        }
    }
</script>

<style scoped>
    .manage-task {
        width: 100%;
        height: 100%;
    }

    /* element ui样式重写 */
    .manage-task > .el-table {
        height: calc(100% - 40px) !important;
    }

    .manage-task > .el-table > .el-table__body-wrapper {
        height: calc(100% - 40px) !important;
    }
</style>